#include "TObject.h"
#include "Utils.h"

TObject::TObject(void)
{
	clear();
}


TObject::~TObject(void)
{
}

const Scalar TObject::colors[] = {Scalar(255,0,0), Scalar(0,255,0), Scalar(0,0,255),
		Scalar(255,255,0), Scalar(255,0,255), Scalar(0,255,255), Scalar(0,0,0), 
		Scalar(255,255,255)};

void TObject::clear(void) {
	rect = NULL;
	center = NULL;
	children.clear();
}

void TObject::drawOn( Mat& img )
{
	if (rect != NULL)
		rectangle(img, *rect, colors[0]);
	if (center != NULL)
		circle(img, *center, 5, colors[1]);
	if (classType != "") {
		int fontFace = FONT_HERSHEY_SIMPLEX;
		double fontScale = 2;
		int thickness = 2;
		int baseline = 0;
		Size textSize = getTextSize(classType, fontFace,
			fontScale, thickness, &baseline);
		baseline += thickness;
		Point textPlace((img.cols - textSize.width) / 2,
			textSize.height);
		putText(img, classType, textPlace, 
			fontFace, fontScale, colors[2], thickness);

	}
		
}
void TObject::drawOn( Mat& img, const Scalar& c1, const Scalar& c2 )
{
	if (rect != NULL)
		rectangle(img, *rect, c1);
	if (center != NULL)
		if (rect != NULL)
			circle(img, *center, 5, c2);
		else
			circle(img, *center, 5, c1);
}

void TObject::draw( Mat& img )
{
	drawOn(img);
	for (unsigned int i = 0; i < children.size(); ++i)
		children[i].draw(img);

}

void TObject::setCenter( Point2f c )
{
	if (center == NULL) {
		shared_ptr<Point2f> tmp(new Point2f(c));
		center = tmp;

	}
	else
		*center = c;

}

void TObject::setRect( Rect r )
{
	if (rect == NULL) {
		shared_ptr<Rect> tmp(new Rect(r));
		rect = tmp;
	}
	else
		*rect = r;

}

void TObject::addChild( TObject c )
{
	children.push_back(c);
}
